const common = require("../../../common/js/common.js");
const controlIcon = {
  stop: "../../../assets/minStop.png",
  play: "../../../assets/minPlay.png"
};
Component({
  behaviors: [],

  properties: {
    showValue: Boolean,
    songInfo: {
      type: Object,
      value: "",
      observer: (newVal, oldVal) => {
        if (newVal.flag ? false : newVal.musicSrc == oldVal.musicSrc || common.customTrim(newVal.musicSrc) == "") {
          return;
        }
        common.bgMusic.bgPlay(
          newVal.musicSrc,
          newVal.songName,
          newVal.singerPic,
          data => {
            console.log(data);
          },
          err => {
            console.log(err);
          }
        );
      }
    }
  },
  data: {
    playList: [],
    playIcon: controlIcon.stop,
    stopFlag: false
  },
  created() {
    console.log("created");
  },
  attached: function () {
    let that = this;
    console.log("attached");
    common.bgMusic.onBgPlay(data => {
      that.setData({
        playIcon: controlIcon.play
      });
      that.triggerEvent('customPlayEvent');
    });
    common.bgMusic.onBgPause(data => {
      that.setData({
        playIcon: controlIcon.stop
      });
      that.triggerEvent('customPauseEvent');
    });
    common.bgMusic.onBgStop(data => {
      console.log("bgstop");
      that.setData({
        playIcon: controlIcon.stop,
        stopFlag: true
      });
      that._stopAudio();
    });
  },
  ready() {
    console.log("ready");
  },
  moved: function () {
    console.log("move");
  },
  detached: function () {
    console.log("detached");
  },
  methods: {
    audioPlay() {
      let that = this;
      if (that.data.stopFlag) {
        if (common.customTrim(that.data.songInfo.musicSrc) != "") {
          common.bgMusic.bgPlay(
            that.data.songInfo.musicSrc, '', '', () => {

            }, (err) => {
              console.log(err);
            }
          );
        }
      } else {
        common.bgMusic.bgPlay();
      }
    },
    _stopAudio() {
      this.triggerEvent('customStopEvent');
    },
    _preSongFun() {
      console.log("上一首music");
      this.triggerEvent('customPreEvent');
    },
    _nextSongFun() {
      console.log("下一首music");
      this.triggerEvent('customNextEvent');
    },
    _playSongFun() {
      console.log("暂停");
      if (this.data.playIcon == controlIcon.stop) {
        this.audioPlay();
      } else {
        common.bgMusic.bgPause();
      }
    }
  }
});
